{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <select name="app_platform_type_id" lay-filter="app_platform_type_id" id="app_platform_type_id" lay-verify="required">
            <option value="">游戏平台（全部）</option>
            {volist name="$app_platform_type_id" id="vo"}
            <option value="{$vo.id}">{$vo.id}：{$vo.name}</option>
            {/volist}
        </select>
    </div>
    <div class="layui-inline">
        <select name="app_client_type_id" lay-filter="app_client_type_id" id="app_client_type_id" lay-verify="required">
            <option value="">游戏终端（全部）</option>
            {volist name="$app_client_type_id" id="vo"}
            <option value="{$vo.id}">{$vo.name}</option>
            {/volist}
        </select>
    </div>
    <div class="layui-inline">
        <select name="game_id" lay-filter="game_id" id="game_id" lay-verify="required" lay-search="">
            <option value="">游戏名称（全部）</option>
            {volist name="$game_id" id="vo"}
            <option value="{$vo.game_id}">{$vo.game_id}：{$vo.game_name}</option>
            {/volist}
        </select>
    </div>
    <div class="layui-inline">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
        <button class="layui-btn my-btn my-btn-sm" type="button" id="add">添加游戏终端</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.game_id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-normal layui-btn-sm"  lay-event="params">参数</a>
    <a class="layui-btn layui-btn-green layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm" style="background: #6495ed;" lay-event="upload">上传</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var jsonObj = {
                        "game_id": $('#game_id').val()
                        ,"app_platform_type_id": $('#app_platform_type_id').val()
                        ,"app_client_type_id": $('#app_client_type_id').val()
                    };

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: jsonObj
                        ,height: 'full-65'
                    })
                }
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-65'
            ,url: '{:url("GameList/client")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function () {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'game_id', title: '游戏ID', width: 100}
                ,{field:'game_name', title: '游戏名称', width: 150}
                ,{field:'app_platform_type_id', title: '游戏平台', width: 100}
                ,{field:'app_client_type_id', title: '游戏终端', width: 150}
                ,{field:'package_size', title: '包大小', width: 80}
                ,{field:'package_name', title: '包名', width: 150}
                ,{field:'up_location', title: '上传位置', width: 150}
                ,{field:'status', title: '状态', width: 100, templet: '#switchTpl'}
                ,{field:'sdk_version', title: 'SDK版本', width: 150}
                ,{field:'h5_game_url', title: 'H5游戏地址', width: 150}
                ,{field:'pay_callback', title: '支付回调', width: 400}
                ,{field:'rebate_callback', title: '返利回调', width: 400}
                ,{field:'gm_callback', title: 'GM回调', width: 400}
                ,{field:'online_time_region', title: '上线开放时间', width: 170}
                ,{field:'plat_show_region', title: '预热开放时间', width: 170}
                ,{field:'charge_time_region', title: '充值开放时间', width: 170}
                ,{field:'reg_time_region', title: '注册开放时间', width: 170}
                ,{field:'login_time_region', title: '登录开放时间', width: 170}
                ,{field:'pack_time_region', title: '打包开放时间', width: 170}
                ,{field:'last_update_time', title: '最后更新时间', width: 170}
                ,{field:'add_time', title: '添加时间', width: 170}
                ,{title: '操作', align:'center', fixed:'right', width: 250, toolbar:'#tableBar'}
            ]]
        });


        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("GameList/clientAdd")}'
            });
        })

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            $.post('{:url("GameList/clientEdit")}',{game_id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("GameList/clientEdit")}?game_id='+data.game_id
                    ,cancel: function(){
                        $.post('{:url("GameList/client")}',{game_id: data.game_id}, function(json){
                            obj.update({
                                game_name: json.data[0].game_name
                                ,app_platform_type_id: json.data[0].app_platform_type_id
                                ,app_client_type_id: json.data[0].app_client_type_id
                                ,up_location: json.data[0].up_location
                                ,sdk_version: json.data[0].sdk_version
                                ,h5_game_url: json.data[0].h5_game_url
                                ,online_time_region: json.data[0].online_time_region
                                ,plat_show_region: json.data[0].plat_show_region
                                ,charge_time_region: json.data[0].charge_time_region
                                ,reg_time_region: json.data[0].reg_time_region
                                ,login_time_region: json.data[0].login_time_region
                                ,pack_time_region: json.data[0].pack_time_region
                                ,last_update_region: json.data[0].last_update_region
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("GameList/clientDel")}',{game_id: data.game_id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if(obj.event === 'upload'){
                layer.open({
                    type: 2
                    ,title: '上传母包'
                    ,offset: '70px'
                    ,resize: true
                    ,area: ['1000px', '650px']
                    ,content: '{:url("GameList/upload")}?game_id='+data.game_id
                });
            }else if(obj.event === 'params'){
                layer.open({
                    type: 1
                    ,title: '参数'
                    ,area: ['800px', '300px']
                    ,shadeClose: true
                    ,offset: '70px'
                    ,shade: 0
                    ,content: '<div style="padding: 20px 40px; font-size: 16px;"><p style="line-height:25px;">游戏名称： '+data.game_name+'</p><p style="line-height:25px;">游戏id(tz_gameid)： '+data.game_id+'</p><p style="line-height:25px;">渠道id(tz_channelid)： 1000</p><p style="line-height:25px;">秘钥(secret)： '+data.secret+'</p><p style="line-height:25px;">module_id： '+data.game_id+'</p></div>'});
            }
        });
    });
</script>
{/block}